package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.UserinfoEntity;
import com.jifeng.br.pjj.bookreader.mapper.UserinfoMapper;
import com.jifeng.br.pjj.bookreader.service.CodeService;
import com.jifeng.br.pjj.bookreader.service.TokenService;
import com.jifeng.br.pjj.bookreader.service.UserinfoService;
import com.jifeng.br.pjj.bookreader.util.R;
import com.jifeng.br.pjj.bookreader.util.exception.RTException;
import com.jifeng.br.pjj.bookreader.util.form.LoginForm;
import com.jifeng.br.pjj.bookreader.util.form.PasswordForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author PangJunjie
 * @Date 2023/7/11/011
 */
@Service
@Transactional
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, UserinfoEntity> implements UserinfoService {

    @Autowired
    private CodeService codeService;

    @Autowired
    private TokenService tokenService;


    @Override
    public R login(LoginForm loginForm) {
        if (!codeService.checkKaptcha(loginForm.getUuid(), loginForm.getCode())) {
            return R.error("验证码不正确！");
        }
        UserinfoEntity loginUser = getByUsername(loginForm.getUsername());
        String md5Pwd = DigestUtils.md5Hex(loginForm.getPassword());
        if (loginUser == null || !loginUser.getPassword().equals(md5Pwd)) {
            return R.error("用户名或密码不正确!");
        }
        if (loginUser.getRole() == 3) {
            return R.error("读者禁止登录后台！");
        }
        if (loginUser.getStatus() == 2) {
            return R.error("该用户已被封号，请联系管理员！");
        }
        return tokenService.saveToken(loginUser.getUserId());
    }

    @Override
    public void update(UserinfoEntity userinfo) {
        if (userinfo.getUserId() == null) {
            if (getByUsername(userinfo.getUsername()) != null) {
                throw new RTException("用户名已存在！");
            }
            userinfo.setPassword(DigestUtils.md5Hex("123456"));
            this.save(userinfo);
        } else {
            this.updateById(userinfo);
        }
    }

    @Override
    public void updatePassword(PasswordForm passwordForm) {
        UserinfoEntity userinfo = this.getById(passwordForm.getUserId());
        if (userinfo == null || !userinfo.getPassword().equals(DigestUtils.md5Hex(passwordForm.getOldPwd()))) {
            throw new RTException("原密码不正确！");
        }
        userinfo.setPassword(DigestUtils.md5Hex(passwordForm.getPassword()));
        this.updateById(userinfo);
    }

    @Override
    public IPage<UserinfoEntity> getPage(int page, int limit, UserinfoEntity userinfo) {
        IPage<UserinfoEntity> iPage = this.page(new Page<>(page, limit), new QueryWrapper<UserinfoEntity>()
                .eq(StringUtils.isNotEmpty(userinfo.getUsername()), "username", userinfo.getUsername())
                .like(StringUtils.isNotEmpty(userinfo.getNickname()), "nickname", userinfo.getNickname())
                .eq(StringUtils.isNotEmpty(userinfo.getPhone()), "phone", userinfo.getPhone())
                .eq("role", 1)
                .eq(userinfo.getStatus() != null, "status", userinfo.getStatus())
                .orderByDesc("user_id")
        );
        return iPage;
    }

    @Override
    public UserinfoEntity getByUsername(String username) {
        return this.getOne(new QueryWrapper<UserinfoEntity>().eq("username", username));
    }
}
